//
//  ProductDetailView.m
//  LoveTravel
//
//  Created by MS on 16-1-25.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "ProductDetailView.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "Product_HeadImageView.h"
#import "UserView.h"
#import "LocationView.h"
#import "InformationView.h"
#import "UIImage+Image.h"
#define HEAD_HEIGHT 200
@interface ProductDetailView ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,weak) UIView * contentView;
@property (nonatomic,weak) Product_HeadImageView * headView;
@property (nonatomic,weak) UserView * userView;
@property (nonatomic,weak) LocationView * mapView;
@property (nonatomic,weak) InformationView * infoView;
@property (nonatomic,weak) UIButton * moreBtn;
@property (nonatomic,weak) UIView * adjustView;
@property (nonatomic,assign) float height;
@end

@implementation ProductDetailView

#pragma mark- 设值
-(void)setProduct_Frame:(Product_Detail_Frame *)product_Frame
{
    _product_Frame = product_Frame;
    
    ProductDetailModel * productData = product_Frame.productData;
    /**
     *  设置头部图片
     */
    self.headView.imageArray  = productData.bg_pic;
    _headView.titleView.text = productData.title;
    _headView.subTitleView.text = productData.sub_title;
    
    
    //设置喜欢的用户
    self.userView.productData = productData;
    
    self.mapView.productData = productData;
    
    /**
     *  设置中间内容
     */
    self.contentView.frame = CGRectMake(0, CGRectGetMaxY(_mapView.frame)+10, self.width, product_Frame.contenView_Height+40);
    
    [self setUpContentViewWithProductData:productData];
    
    //设置底部infoView
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(heightTol:) name:@"CellTolHeight" object:nil];
   
    self.infoView.productData = productData;
    
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(_infoView.frame)+10);
}

-(void)heightTol:(NSNotification *)note
{
    _height = [note.userInfo[@"CellTolHeight"] floatValue] + 40*3+20;
    [self layoutSubviews];
}


#pragma mark- 设置中间内容
-(void)setUpContentViewWithProductData:(ProductDetailModel *)productData
{
    for (Product_Details * detail in productData.details) {
        
        if ([detail.type isEqualToString:@"head"]) {
            
            UILabel * title = [[UILabel alloc] initWithFrame:detail.content.headFrame];
            [_contentView addSubview:title];
            title.textColor = [UIColor blackColor];
            title.textAlignment = NSTextAlignmentCenter;
            title.text = detail.content.head;
        }
        
        if ([detail.type isEqualToString:@"referrer"]) {
            UIView * view = [[UIView alloc] initWithFrame:detail.content.referrerFrame];
            [_contentView addSubview:view];
            
            UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
            imgV.layer.cornerRadius = 25;
            imgV.clipsToBounds  =YES;
            imgV.center = CGPointMake(view.width*0.5, 25);
            [imgV sd_setImageWithURL:[NSURL URLWithString:productData.referrer.photo_url]];
            [view addSubview:imgV];
            
            UILabel * nameV = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, view.width, 20)];
            [view addSubview:nameV];
            nameV.textAlignment = NSTextAlignmentCenter;
            nameV.textColor = [UIColor grayColor];
            nameV.font = [UIFont systemFontOfSize:12];
            nameV.text = productData.referrer.username;
            
        }
        
        if ([detail.type isEqualToString:@"text"]) {
            
            UILabel * textV = [[UILabel alloc] initWithFrame:detail.content.textFrame];
            [_contentView addSubview:textV];
            textV.numberOfLines = 0;
            textV.textColor = [UIColor colorWithRed:0.32f green:0.32f blue:0.40f alpha:1.00f];
            textV.font = [UIFont systemFontOfSize:TEXT_FONT];
            textV.text = detail.content.text;
        }
        
        if ([detail.type isEqualToString:@"pic"]) {
            
            UIImageView * imgV = [[UIImageView alloc] initWithFrame:detail.content.picFrame];
            [imgV sd_setImageWithURL:[NSURL URLWithString:detail.content.url]];
            [_contentView addSubview:imgV];
        }
    }
    
    self.adjustView.frame = CGRectMake(0,_product_Frame.contenView_Height, self.width, 40);
    
    [self adjustContentView:_moreBtn];
}

#pragma mark- 懒加载地点视图
-(LocationView *)mapView
{
    if (_mapView==nil) {
        LocationView * view = [LocationView mapViewWithXib];
        _mapView = view;
        _mapView.frame = CGRectMake(0, CGRectGetMaxY(_userView.frame)+10, self.width, 60);
        [self.scrollView addSubview:view];
    }
    return _mapView;
}

#pragma mark- 懒加载喜欢用户视图
-(UserView *)userView
{
    if (_userView==nil) {
        UserView * view = [UserView userViewWithXib];
        _userView = view;
        [self.scrollView addSubview:view];
        _userView.frame = CGRectMake(0, CGRectGetMaxY(_headView.frame), self.width, 50);
    }
    return _userView;
}

#pragma mark- 懒加载头部视图
-(Product_HeadImageView *)headView
{
    if (_headView==nil) {
        Product_HeadImageView * view = [Product_HeadImageView productImageViewWithXib];
        view.frame = CGRectMake(0, 0, self.width, HEAD_HEIGHT);
        _headView = view;
        [self.scrollView addSubview:view];
    }
    return _headView;
}

-(InformationView *)infoView
{
    if (_infoView==nil) {
        InformationView * view  = [[InformationView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_contentView.frame)+10, self.width, 800)];
        
        _infoView = view;
        _infoView.backgroundColor = [UIColor redColor];
        [self.scrollView addSubview:view];
    }
    return _infoView;
}

#pragma mark- 初始化，添加scrollView、中间视图、中间视图上得“更多内容”按钮
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:frame];
        self.scrollView = scrollView;
        scrollView.delegate = self;
        self.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [self addSubview:scrollView];
        
        UIView * contentView = [[UIView alloc] init];
        [scrollView addSubview:contentView];
        contentView.backgroundColor = [UIColor whiteColor];
        self.contentView = contentView;
        contentView.clipsToBounds = YES;
        
        UIView * adjustView = [[UIView alloc] init];
        [contentView addSubview:adjustView];
        adjustView.backgroundColor = [UIColor whiteColor];
        UIButton * adjustBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.moreBtn = adjustBtn;
        [adjustView addSubview:adjustBtn];
        adjustBtn.frame = CGRectMake(0, 10, self.width, 20);
        
        [adjustBtn setTitleColor:[UIColor colorWithRed:0.32f green:0.32f blue:0.40f alpha:1.00f] forState:UIControlStateNormal];
        adjustBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        self.adjustView = adjustView;
        
        [adjustBtn setTitle:@"收起内容" forState:UIControlStateNormal];
         [adjustBtn setImage:[UIImage imageNamed:@"activity_moreD2"] forState:UIControlStateNormal];
        [adjustBtn setTitle:@"更多内容" forState:UIControlStateSelected];
        [adjustBtn setImage:[UIImage imageNamed:@"activity_moreD"] forState:UIControlStateSelected];
        adjustBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 100, 0, 0);
        adjustBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
        [adjustBtn addTarget:self action:@selector(adjustContentView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

#pragma mark- 内容收缩处理
-(void)adjustContentView:(UIButton *)btn
{
    [_contentView bringSubviewToFront:_adjustView];
    btn.selected = !btn.selected;
    if (btn.selected) {
        _contentView.height = _product_Frame.adjust_Height+40;
        _adjustView.y = _product_Frame.adjust_Height;
    }
    else{
        _contentView.height = _product_Frame.contenView_Height+40;
        _adjustView.y = _product_Frame.contenView_Height;

    }
    
    [self layoutSubviews];
}

#pragma mark- 处理下拉头部视图效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.y;
  
    if (offset<0) {
        self.headView.y = offset;
        self.headView.height = HEAD_HEIGHT-offset;
    }
    if (offset<=-70) {
        
        self.scrollView.contentOffset =CGPointMake(0,-70);
    }
    
    /**
     *  发送标题改变的通知
     */
    BOOL titleChange = offset>=HEAD_HEIGHT-64? NO : YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SetNav" object:nil userInfo:@{@"NavChange":@(titleChange)}];
    
}

#pragma mark- 处理头部视图内部定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_headView.adsView stopTime];
    
//    if (scrollView.contentOffset.y<30) {
//        [self.headView.adsView autoScrollWithEvent];
//
//    }
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   
    [_headView.adsView startTime];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _infoView.frame = CGRectMake(0, CGRectGetMaxY(_contentView.frame)+10, self.width, _height);
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(_infoView.frame));
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
